package com.pengcheng.jd.util;


import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component
public class HttpUtils {

    private PoolingHttpClientConnectionManager cm;

    public HttpUtils() {
        this.cm = new PoolingHttpClientConnectionManager();
    }

    /**
     * 根据请求地址下载页面数据
     * @param url
     * @return 页面数据
     */
    public String doGetHtml(String url) {
        // 获取HttpClient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(this.cm).build();

        // 设置最大连接数
        this.cm.setMaxTotal(100);

        // 设置每个主机的最大连接数
        this.cm.setDefaultMaxPerRoute(10);

        // 设置httpGet请求对象，设置url地址
        HttpGet httpGet = new HttpGet(url);

        // 设置请求信息
        httpGet.setConfig(this.getConfig());
        httpGet.setHeader(":authority","list.jd.com");
        httpGet.setHeader(":method","GET");
        httpGet.setHeader(":path","/list.html?cat=9987%2C653%2C655&page=3&s=57&click=0");
        httpGet.setHeader(":scheme","https");
        httpGet.setHeader("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        httpGet.setHeader("accept-encoding","gzip, deflate, br");
        httpGet.setHeader("accept-language","zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        httpGet.setHeader("cache-control","max-age=0");
        httpGet.setHeader("cookie","unpl=V2_ZzNtbUQDEBN9CUUGck4IB2JREw1KUBBCfVhOU30QVAVlBBdaclRCFnQURlRnGlgUZgIZXEpcRxBFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZHgYWgNkBRJcS2dzEkU4dlZ7EVkNYTMTbUNnAUEpDEJTfxtcSGQCFFtBUUMUfDhHZHg%3d; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_7eb7902c8fe34c0a96bf9a9678913646|1606234194637; __jdu=207727108; areaId=2; PCSYCityID=CN_310000_310100_310112; shshshfpa=39280ba5-7cf2-ebf6-5389-59c758f49efb-1606234195; __jdc=122270672; shshshfp=7b7dd83bce6abbbb3371118f9cf25cdb; shshshfpb=a3Wj4W662MBxt%202ew8PrbTg%3D%3D; 3AB9D23F7A4B3C9B=KFWGOOVACKCJ6ITB7SGFXVETQFOVJRV4TNYORM5UAVZAI5WY7KU6XEXOORFWHBXL2FC4TEZ2TUCR2B7BMTKYNH26QM; ipLoc-djd=2-2825-51931-0; __jda=122270672.207727108.1606234194.1606234195.1606345081.2; __jdb=122270672.5.207727108|2.1606345081; shshshsID=59fef4739aadee76d481350fde8abb21_4_1606347602668");
        httpGet.setHeader("referer","https://search.jd.com/");
        httpGet.setHeader("sec-fetch-dest","document");
        httpGet.setHeader("sec-fetch-mode","navigate");
        httpGet.setHeader("sec-fetch-site","same-origin");
        httpGet.setHeader("sec-fetch-user","?1");
        httpGet.setHeader("upgrade-insecure-requests","1");
        httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36 Edg/87.0.664.41");

        // 使用HttpClient发起请求，获取响应
        CloseableHttpResponse response = null;

        String content = "";
        try {
            response = httpClient.execute(httpGet);

            // 解析响应，返回结果
            if (response.getStatusLine().getStatusCode() == 200 ) {
                // 判断响应体Entity是否不为空，如果不为空就可以使用EntityUtils
                if (response.getEntity() != null) {
                    content  = EntityUtils.toString(response.getEntity(), "utf-8");
                    return content;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return content;
    }

    // 设置请求的信息
    private RequestConfig getConfig() {
        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(1000) // 创建连接的最长时间
                .setConnectionRequestTimeout(500) // 获取连接的最长时间
                .setSocketTimeout(1000)
//                .setDefaultKeepAlive(10000, TimeUnit.MICROSECONDS) // 长连接的时间
                .build();
        return config;
    }


    /**
     * 下载图片
     * @param url
     * @return 图片名称
     */
    public String doGetImage(String url) {
        // 获取HttpClient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(this.cm).build();

        // 设置最大连接数
        this.cm.setMaxTotal(100);

        // 设置每个主机的最大连接数
        this.cm.setDefaultMaxPerRoute(10);

        // 设置httpGet请求对象，设置url地址
        HttpGet httpGet = new HttpGet(url);

        // 设置请求信息
        httpGet.setConfig(this.getConfig());
//        httpGet.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
//        httpGet.setHeader("Accept-Encoding","gzip, deflate");
//        httpGet.setHeader("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
//        httpGet.setHeader("Cache-Control","max-age=0");
//        httpGet.setHeader("Connection","keep-alive");
//        httpGet.setHeader("Host","img10.360buyimg.com");
//        httpGet.setHeader("If-Modified-Since","Thu, 03 Sep 2020 06:14:04 GMT");
//        httpGet.setHeader("Upgrade-Insecure-Requests","1");
//        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36 Edg/87.0.664.41");

        // 使用HttpClient发起请求，获取响应
        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpGet);

            int code = response.getStatusLine().getStatusCode();
            System.out.println("code = " + code);
            // 解析响应，返回结果
            if (response.getStatusLine().getStatusCode() == 200 ) {
                // 判断响应体Entity是否不为空，如果不为空就可以使用EntityUtils
                if (response.getEntity() != null) {
                    // 下载图片
                    // 获取图片的后缀
                    String extName = url.substring(url.lastIndexOf("."));

                    // 创建图片名，重命名图片
                    String picName = UUID.randomUUID().toString() + extName;

                    // 下载图片
                    // 声明OutPutStream
                    OutputStream outputStream = new FileOutputStream(new File("C:\\Users\\admin\\Desktop\\资料\\crawler-jd-images\\"+picName));
                    response.getEntity().writeTo(outputStream);

                    // 返回图片名称
                    return picName;

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        // 如果失败返回空串
        return "";

    }
}
